export default {
	
	data(){
		return {
			readySelecteds: [],
			doneSelecteds: []
		}
	}, 
	computed:{
		// enrollState() {
		// 	let that = this
		// 	return  function(enroll) {
		// 		if(!enroll.assigned_job_at){
		// 			return 1
		// 		}else if(!enroll.sign_up_at) { 
		// 			return 2
		// 		}else if (!enroll.sign_down_at){
		// 			return 3
		// 		}else if (!enroll.verified_at){
		// 			return 4
		// 		}else if (!enroll.give_salary_at){
		// 			return 5
		// 		}else
		// 		{
		// 			return 6
		// 		} 
		// 	}
		// },
		isOK()
		{
			let that = this
			return function(enroll,index){
				let s = enroll.state_step
				return s >= index + 2
				//return true
			}
		},
		// enrollsByState(){
		// 	let that = this
		// 	return function(enrolls,state,job_id)
		// 	{
		// 		let arr = enrolls.filter((enroll)=>{
		// 			if(job_id && enroll.expected_job_id != job_id) return false
					
		// 			let enrollState = enroll.state_step
					
		// 			if( enrollState >= state + 1)
		// 				return true
						
		// 			return false
		// 		}) 
				
		// 		return arr
		// 	}
		// },
		enrollsByState(){
			let that = this
			return function(enrolls,index,job_id=null)
			{
				let arr = enrolls.filter((enroll)=>{
					if(job_id)if(enroll.expected_job_id != job_id) return false
					
					let enrollState = enroll.state_step
					
					if( enrollState >= index + 2) 
						return true
						
					return false
				}) 
				
				return arr
			}
		},
		enrollsForReady(){
			let that = this
			return function(enrolls,index,job_id=null)
			{
				let arr = enrolls.filter((enroll)=>{
					if(job_id)if(enroll.expected_job_id != job_id) return false
					
					let enrollState = enroll.state_step
					
					if( enrollState == index) 
						return true
						
					return false
				}) 
				
				return arr
			}
		},
		enrollsForDone(){
			let that = this
			return function(enrolls,index,job_id=null)
			{
				let arr = enrolls.filter((enroll)=>{
					if(job_id)if(enroll.expected_job_id != job_id) return false
					
					let enrollState = enroll.state_step
					
					if( enrollState >= index) 
						return true
						
					return false
				}) 
				
				return arr
			}
		},
		stateCode(){
			return (enroll)=>{
				if(!enroll.rejected_at && !enroll.kickout_at && !enroll.assigned_job_at)
				{
					return 1 //raw
				}else if(enroll.rejected_at || enroll.kickout_at) {
					return 2
				}else if(enroll.assigned_job_at)
				{
					return 0 
				}
			}
		},
		enrollRawsCount (){
			return (enrolls)=>{
				return enrolls.filter(enroll=>this.stateCode(enroll) == 1).length
			}
		},
		leaders(){
			let arr = this.enrolls.filter(enroll=>!!enroll.is_leader)
			return arr
		},
		labelText(){
			let that = this
			return function(enroll,index){
				let isOK = that.isOK(enroll,index) //that.enrollState(enroll) > (index - 1)
				
					 if(index == 0) {return isOK ? '已确认' : '待确认'}
				else if(index == 1) {return isOK ? '已签到' : '可签到'}
				else if(index == 2) {return isOK ? '已签退' : '可签退'}
				else if(index == 3) {return isOK ? '已验收' : '可验收'}
				else if(index == 4) {return isOK ? '已发薪' : '可发薪'} 
			}
		},
		labelColor(){
			let that = this
			return function(enroll,index){
				return that.isOK(enroll,index) ? 'white' : '#666666'
			}
		},
		labelBackgroundColor(){
			let that = this
			return function(enroll,index){
				return that.isOK(enroll,index) ? '#09c31e' : 'white'
			}
		},
		isSelected() {
			return (selecteds,enroll) => {
				return selecteds.find((id) => enroll.id == id) > -1
			}
		}
	},
	methods:{
		async operate(type,enroll_ids,enrolls,params={}){
			uni.showLoading({
				title:'正在操作'
			})

			let res = await this.$http.post('/manager/job/enrolls/assign',{
				enroll_ids,
				type,
				...params
			})
			
			console.log(enrolls)
			console.log(res.data)
			res.data.forEach(enroll=>{
				console.log(enroll.id)
				let index = enrolls.findIndex(e=>e.id == enroll.id)	
				console.log(index)
				if(index < 0) return
				enrolls.splice(index,1,enroll)
			})
			 
			uni.hideLoading()
		},
		select(selecteds,enroll) {
			if (this.isSelected(selecteds,enroll)) {
				selecteds.splice(selecteds.findIndex(id=>id==enroll.id),1)
			} else {
				selecteds.push(enroll.id)
			}
		},
		selectAll(enrolls,selecteds) {
			if(selecteds.length > 0)
			{
				selecteds.splice(0,selecteds.length)
			}
			else 
			{
				selecteds.push(...enrolls.map(enroll => enroll.id))
			}
		},
		updateEnrolls(datas) {
			datas = datas.map(data => {
				var index = this.task.enrolls.findIndex(enroll => {
					return enroll.id == data.id
				})
		
				return {
					index,
					data
				}
			})
		
			console.log(datas)
		
			datas.forEach(data => {
				var tmp = null
				if (data.index >= 0)
					tmp = Object.assign(this.task.enrolls[data.index], data.data)
				else
					tmp = data.data
		
				this.task.enrolls.splice(data.index, 1,tmp)
			})
		},
		async onScheduleJob(job_id)
		{
			await this._operate('assign',{job_id})
		},
		async onSignUp(){
			await this._operate('signup')
		},
		async onSignDown(){
			await this._operate('signdown')
		},
		async onVerifiy(){
			await this._operate('verified')
		}, 
		async onGiveSalary(){
			await this._operate('give_salary')
		}, 
		async onAssignLeader(){
			await this._operate('is_leader',{is_leader:true})
		}, 
		async onUnassignLeader(){
			await this._operate('is_leader',{is_leader:false})
		},
		async onClearance(){
			await this._operate('signdown')
		}
	}
}